package io;

import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_DaChiZi
 * Date: 2025/7/4
 * Time: 12:48
 */
public class Demo17 {
    //实现查找+删除文件操作
    public static void doDelete(File f, String key) {
        if (!f.getName().contains(key)) {
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("是否确认删除 Y/N");
        String choice = scanner.next();
        if (choice.equals("Y") || choice.equals("y")) {
            f.delete();
        }
    }

    public static void scan(File currentFile, String key) {
        if (!currentFile.isDirectory()) {
            return;
        }
        File[] files = currentFile.listFiles();
        if (files == null || files.length == 0) {
            return;
        }

        for (File f : files) {
            if (f.isFile()) {
                System.out.println(f.getName());
                doDelete(f,key);
            } else {
                scan(f,key);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("请输入要搜索的路径");
        Scanner scanner = new Scanner(System.in);
        String rootPath = scanner.next();
        File rootFile = new File(rootPath);
        if (!rootFile.isDirectory()) {
            System.out.println("路径不存在");
            return;
        }
        System.out.println("请输入要删除的文件名的关键词");
        String key = scanner.next();

        scan(rootFile,key);
    }
}
